class EzReq {
    public cmd:number;
    public resolve:any;//同步等待时，处理完的回调
    public reqStart:number =0;//请求开始时间
    public reqEnd:number =0;//请求结束时间
    public msgSerialNumber;//消息序列号

    constructor(cmd) {
        this.cmd = cmd
    }

    onError(callback) {
        this.errorDelegate = new class {
            onError(result) {
                callback(result);
            }
        };
        return this;
    }
    onCallback(callback) {
        // result: ResponseResult
        this.callbackDelegate = new class {
            onCallback(result) {
                callback(result);
            }
        };
        return this;
    }
    do() {
        if (this.requestExecuteType != RequestExecuteType.none)
            return this;
        this.requestExecuteType = RequestExecuteType.execute;
        CommandManager.writeAndFlush(this);
        return this;
    }
    internalWakeup(message) {
        if (!this.isExecuteType(RequestExecuteType.executeAwait)) {
            return false;
        }
        if (this.resolve) {
            const result = ResponseResult.of(message, this);
            this.resolve(result);
        }
        return true;
    }
    acceptMessage(result) {
        var _a;
        if (this.requestExecuteType === RequestExecuteType.execute) {
            (_a = this.callbackDelegate) === null || _a === void 0 ? void 0 : _a.onCallback(result);
        }
    }
    static async ofAwaitRequestCommand(requestCommand) {
        requestCommand.requestExecuteType = RequestExecuteType.executeAwait;
        return new Promise(resolve => {
            // see method acceptMessage
            requestCommand.resolve = resolve;
            CommandManager.writeAndFlush(requestCommand);
        });
    }
    static of(cmdMerge, data) {
        const requestCommand = new RequestCommand(cmdMerge);
        requestCommand.data = data;
        return requestCommand;
    }
    static ofInt(cmdMerge, data) {
        const wrapData = WrapKit.ofInt(data);
        const request = this.of(cmdMerge, WrapKit.ofBinaryForInt(wrapData));
        request.dataSource = wrapData;
        return request;
    }
    static ofIntList(cmdMerge, data) {
        const wrapData = WrapKit.ofIntList(data);
        const request = this.of(cmdMerge, WrapKit.ofBinaryForIntList(wrapData));
        request.dataSource = wrapData;
        return request;
    }

}

export class EzRequest {
    msgId: number;
    cmd: number;
    errorDelegate?: ErrorDelegate;
    callbackDelegate?: CallbackDelegate;
    data: any;
    /** 原始的数据，没有编码的 */
    dataSource: any;
    requestExecuteType: RequestExecuteType;
    timeStart: number;
    timeConsumer?: number;
    private constructor();
    countTimeConsumer(): number;
    isExecuteType(requestExecuteType: RequestExecuteType): boolean;
    onError(callback: (result: ResponseResult) => void): RequestCommand;
    onCallback(callback: (result: ResponseResult) => void): RequestCommand;
    execute(): RequestCommand;
    internalWakeup(message: NetExternalMessage): boolean;
    acceptMessage(result: ResponseResult): void;
    static ofAwaitRequestCommand(requestCommand: RequestCommand): Promise<ResponseResult>;
    static of(cmdMerge: number): RequestCommand;
    static of(cmdMerge: number, data: Uint8Array): RequestCommand;
    static ofInt(cmdMerge: number, data: number): RequestCommand;
    static ofIntList(cmdMerge: number, data: number[]): RequestCommand;
    static ofBool(cmdMerge: number, data: boolean): RequestCommand;
    static ofBoolList(cmdMerge: number, data: boolean[]): RequestCommand;
    static ofString(cmdMerge: number, data: string): RequestCommand;
    static ofStringList(cmdMerge: number, data: string[]): RequestCommand;
    static ofLong(cmdMerge: number, data: bigint): RequestCommand;
    static ofLongList(cmdMerge: number, data: bigint[]): RequestCommand;
    static ofValueList(cmdMerge: number, dataList: Uint8Array[]): RequestCommand;
    static ofAwait(cmdMerge: number): Promise<ResponseResult>;
    static ofAwait(cmdMerge: number, data: Uint8Array): Promise<ResponseResult>;
    static ofAwaitInt(cmdMerge: number, data: number): Promise<ResponseResult>;
    static ofAwaitIntList(cmdMerge: number, data: number[]): Promise<ResponseResult>;
    static ofAwaitBool(cmdMerge: number, data: boolean): Promise<ResponseResult>;
    static ofAwaitBoolList(cmdMerge: number, data: boolean[]): Promise<ResponseResult>;
    static ofAwaitString(cmdMerge: number, data: string): Promise<ResponseResult>;
    static ofAwaitStringList(cmdMerge: number, data: string[]): Promise<ResponseResult>;
    static ofAwaitLong(cmdMerge: number, data: bigint): Promise<ResponseResult>;
    static ofAwaitLongList(cmdMerge: number, data: bigint[]): Promise<ResponseResult>;
    static ofAwaitValueList(cmdMerge: number, dataList: Uint8Array[]): Promise<ResponseResult>;
}